﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NegevGas.Core;
using OSGeo.MapGuide;

namespace NegevGas.Controls
{
    public partial class MaintenanceControl : System.Web.UI.UserControl
    {
        private bool _activated;
        private Feature _feature;
        private HtzMgHelper _helper;
        private string _infraType;
        private HtzMaintenanceHelper _maintHelper;
        private MgLayer _parentLayer;
        private int _uid;
        //protected Button btnAdd;
        //protected Button btnDelete;
        //protected DropDownList ddlInfraType;
        //protected FeatureLogControl featureLogControl;
        //protected FeatureEditorControl MaintenanceEditorControl;
        //protected Panel panelDeleteEntry;
        //protected Panel panelNewEntry;

        public event EventHandler RefreshRequired;

        public void Activate(HtzMgHelper helper, string uidKey, int uid, MgLayer parentLayer)
        {
            if (helper != null)
            {
                this._helper = helper;
                this._uid = uid;
                this._parentLayer = parentLayer;
                this._maintHelper = new HtzMaintenanceHelper(this._helper, parentLayer, uidKey);
                this._activated = this._maintHelper.ModuleAllowed;
            }
            else
            {
                this._activated = false;
            }
        }

        protected void AddNewFeature(object sender, EventArgs e)
        {
            this._maintHelper.CreateNew(this._uid, this.ddlInfraType.SelectedValue);
            this.GetMaintenanceFeature();
            DetailsView dv = (DetailsView)this.MaintenanceEditorControl.FindControl("_FeatureCard");
            dv.ChangeMode(DetailsViewMode.Edit);
            dv.DataBind();
        }

        private void ChangeMode(bool updateMode)
        {
            this.panelNewEntry.Visible = !updateMode;
            this.panelDeleteEntry.Visible = updateMode;
            this.MaintenanceEditorControl.Visible = updateMode;
            this.featureLogControl.Visible = updateMode;
        }

        protected void DeleteFeature(object sender, EventArgs e)
        {
            if (this._feature != null)
            {
                this._maintHelper.DeleteFeature(this._feature.FeatId);
                this.ChangeMode(false);
            }
        }

        private void ForceRefresh()
        {
            if (this.RefreshRequired != null)
            {
                this.RefreshRequired(this, EventArgs.Empty);
            }
        }

        private void GetMaintenanceFeature()
        {
            string maintLayerName = string.Empty;
            foreach (MgLayerBase layer in this._helper.Map.GetLayers())
            {
                if (layer.GetName().StartsWith("Maint_"))
                {
                    maintLayerName = layer.GetName();
                    break;
                }
            }
            if (string.IsNullOrEmpty(maintLayerName))
            {
                this.ChangeMode(false);
            }
            else
            {
                this._feature = Feature.GetSingleFeature(this._helper, string.Format("UID={0} AND FeatureSource='{1}'", this._uid, this._parentLayer.GetFeatureSourceId()), maintLayerName);
                if ((this._feature == null) || (this._feature.UID == 0))
                {
                    this.ChangeMode(false);
                }
                else
                {
                    this.ChangeMode(true);
                    this._feature.Properties["OperationType"].AviableValues = new List<string>();
                    this._feature.Properties["OperationType"].AviableValues.Add("לא מוגדר");
                    this._feature.Properties["OperationType"].AviableValues.Add("שבר");
                    this._feature.Properties["OperationType"].AviableValues.Add("מונעת");
                    this._feature.Properties["OperationType"].AviableValues.Add("מתוכננת");
                    this._feature.Properties["Status"].AviableValues = new List<string>();
                    this._feature.Properties["Status"].AviableValues.Add("פתוח");
                    this._feature.Properties["Status"].AviableValues.Add("סגור");
                    this._feature.Properties["Status"].AviableValues.Add("בטיפול");
                    this._feature.Properties["Status"].AviableValues.Add("מושהה");
                    this._feature.Properties["InfrastructureType"].IsReadOnly = true;
                    this._feature.InsertToLog = true;
                    FeatureLog log = new FeatureLog(this._helper, this._feature);
                    this.featureLogControl.LogSource = log;
                    this.MaintenanceEditorControl.Filter = string.Format("FeatId={0}", this._feature.FeatId);
                    this.MaintenanceEditorControl.Activate(this._helper, this._feature);
                    this.MaintenanceEditorControl.Visible = true;
                    this.MaintenanceEditorControl.FeatureCard_Init();
                }
            }
        }

        protected void MaintenanceEditorControl_RefreshRequired(object sender, EventArgs e)
        {
            this.ForceRefresh();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.MaintenanceEditorControl.RefreshRequired += new EventHandler(this.MaintenanceEditorControl_RefreshRequired);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (this._activated)
            {
                if (this._maintHelper.NewLayerAttached)
                {
                    this.ForceRefresh();
                }
                this.GetMaintenanceFeature();
                if (!base.IsPostBack)
                {
                    foreach (KeyValuePair<string, string> item in this._maintHelper.InfrastructureTypes)
                    {
                        this.ddlInfraType.Items.Add(item.Value);
                    }
                    this._infraType = this._maintHelper.GetInfrastuctureType(this._parentLayer.GetFeatureSourceId());
                    if (string.IsNullOrEmpty(this._infraType))
                    {
                        this._infraType = this._maintHelper.InfrastructureTypes["none"];
                    }
                    else
                    {
                        this.ddlInfraType.Enabled = false;
                    }
                    this.ddlInfraType.SelectedIndex = this.ddlInfraType.Items.IndexOf(this.ddlInfraType.Items.FindByValue(this._infraType));
                    this.ddlInfraType.DataBind();
                }
            }
        }

        public bool Activated
        {
            get
            {
                return this._activated;
            }
        }
    }
}